﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using jTCodeGenerate.Models;

namespace jTCodeGenerate.UserForms
{
    public partial class AddOrEditTemplateForm : Form
    {
        public AddOrEditTemplateForm()
        {
            InitializeComponent();
        }
        private TemplateMdoel _templateMdoel;
        public AddOrEditTemplateForm(TemplateMdoel _templateMdoel)
        {
            InitializeComponent();
            this._templateMdoel = _templateMdoel;
            var fullName = Path.Combine(Directory.GetCurrentDirectory(), _templateMdoel.RelativePath, _templateMdoel.Name);
            if (File.Exists(fullName))
            {
                var txt = File.ReadAllText(fullName);
                this.txt_template_content.Text = txt;
            }
        }
        private void btn_close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_template_save_Click(object sender, EventArgs e)
        {
            var templateContent = this.txt_template_content.Text;
            if (string.IsNullOrWhiteSpace(templateContent))
            {
                MessageBox.Show("模板内容不能为空！");
                return;
            }
            var fullName = Path.Combine(Directory.GetCurrentDirectory(), _templateMdoel.RelativePath, _templateMdoel.Name);

            File.WriteAllText(fullName, templateContent, System.Text.Encoding.UTF8);
        }

        private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string str = txt_template_content.ActiveTextAreaControl.SelectionManager.SelectedText;
            if (str == "") return;
            Clipboard.SetText(str);
            txt_template_content.ActiveTextAreaControl.SelectionManager.RemoveSelectedText();
        }

        private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string str = txt_template_content.ActiveTextAreaControl.SelectionManager.SelectedText;
            if (str == "") return;
            Clipboard.SetText(str);
        }

        private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string str = Clipboard.GetText();
            txt_template_content.ActiveTextAreaControl.TextArea.InsertString(str);

            if (txt_template_content.ActiveTextAreaControl.SelectionManager.HasSomethingSelected)
                txt_template_content.ActiveTextAreaControl.SelectionManager.RemoveSelectedText();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string str = txt_template_content.ActiveTextAreaControl.SelectionManager.SelectedText;
            if (str != "") txt_template_content.ActiveTextAreaControl.SelectionManager.RemoveSelectedText();
        }

        private void 全选ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.txt_template_content.ActiveTextAreaControl.SelectionManager.SetSelection(
                txt_template_content.Document.OffsetToPosition(0),
                txt_template_content.Document.OffsetToPosition(txt_template_content.Document.TextLength));
        }
    }
}
